package cn.com.egova.demoapplication;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.view.animation.Animation;
import android.widget.Button;
import android.widget.FrameLayout;

import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;

public class ActivityAnimationActivity extends FragmentActivity {

    @InjectView(R.id.btn1)
    Button btn1;
    @InjectView(R.id.btn2)
    Button btn2;
    @InjectView(R.id.btn3)
    Button btn3;
    @InjectView(R.id.fl)
    FrameLayout fl;
    @InjectView(R.id.btn_go)
    Button btnGo;

    Animation enterAnima;
    Animation exitAnima;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_animation);
        ButterKnife.inject(this);

//        enterAnima = AnimationUtils.loadAnimation(this,R.anim.animation_rotate);
//        exitAnima = AnimationUtils.loadAnimation(this,R.anim.animation_alpha)
;    }

    @OnClick({R.id.btn_go,R.id.btn1, R.id.btn2, R.id.btn3, R.id.fl})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn_go:
                startActivity(new Intent(this,TypeEvaluatorActivity.class));
                //只有这一个api,故只能用xml配置的Tween动画.
                overridePendingTransition(R.anim.animation_rotate,R.anim.animation_alpha);
                break;
            case R.id.btn1:
                Fragment fragment1 = TestFragment.newInstance("Fragment1");
                switchFragment(fragment1);
                break;
            case R.id.btn2:
                Fragment fragment2 = TestFragment.newInstance("Fragment2");
                switchFragment(fragment2);
                break;
            case R.id.btn3:
                Fragment fragment3 = TestFragment.newInstance("Fragment3");
                switchFragment(fragment3);
                break;
            case R.id.fl:
                break;
        }

    }


    /**
     * 切换Fragment.
     * @param fragment
     */
    private void switchFragment(Fragment fragment) {
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        //Fragment动画一:标准转场动画(系统内置)
//        transaction.setTransition(FragmentTransaction.TRANSIT_NONE);           //无动画
//        transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);   //打开时动画
//        transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_CLOSE);  //关闭时动画

        //Fragment动画一:自定义转场动画(一定要在add,remove,replace之前).
        transaction.setCustomAnimations(R.anim.animation_rotate,R.anim.animation_alpha);
        transaction.replace(R.id.fl,fragment);

        //如果不是replace而是add则可以指定添加到BackStack.
//        transaction.add(R.id.fl,fragment);
//        transaction.addToBackStack("frag");

        transaction.commit();
    }




    @Override
    public void onBackPressed() {
        super.onBackPressed();
        //startActiivty和onBackPressed以及finish()等方法后都要执行下面这一行代码才会有Activity动画效果.
        overridePendingTransition(R.anim.animation_rotate,R.anim.animation_alpha);
    }


    @Override
    public void finish() {
        super.finish();
        overridePendingTransition(R.anim.animation_rotate,R.anim.animation_alpha);
    }
}
